const TokenKey = 'pet_platform_token'
const UserKey = 'pet_platform_user'

export function getToken() {
  const token = localStorage.getItem(TokenKey)
  return token && token !== 'undefined' ? token : null
}

export function setToken(token) {
  if (token && token !== 'undefined') {
    localStorage.setItem(TokenKey, token)
  }
}

export function removeToken() {
  localStorage.removeItem(TokenKey)
}

export function getUser() {
  try {
    const userStr = localStorage.getItem(UserKey)
    return userStr && userStr !== 'null' ? JSON.parse(userStr) : null
  } catch (error) {
    console.error('Error parsing user data:', error)
    return null
  }
}

export function setUser(user) {
  if (user) {
    localStorage.setItem(UserKey, JSON.stringify(user))
  }
}

export function removeUser() {
  localStorage.removeItem(UserKey)
}

export function clearAuth() {
  removeToken()
  removeUser()
}

export function isAuthenticated() {
  const token = getToken()
  const user = getUser()
  return !!(token && user)
} 